<!DOCTYPE html>
<meta charset="UTF-8">
<title>Fragment navigation on initial empty document created through window.open(url-with-204-response)</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/helpers.js"></script>
<body></body>
<script>
/*
  When a new window is opened through window.open() it will contain the initial
  empty document. If the URL parameter is set to the URL that doesn't load a
  new document (e.g. it results in a HTTP 204 response), it will stay on the
  initial empty document. If history.pushState() or history.replaceState() is
  called on it, it should still stay on the initial empty document.
  These tests verify the behavior of navigations that happen on the initial
  empty document in that situation. They should all be converted to do a
  replacement.
*/
"use strict";
const url1 = "about:blank#foo";
const url2 = "resources/code-injector.html?2&pipe=sub(none)&code=" +
              encodeURIComponent(postMessageToOpenerOnLoad);

promise_test(async t => {
  // Open a new window with a URL that doesn't load a new document, so it will stay in the initial empty document.
  const openedWindow = windowOpen204(t);

  // Do a history.pushState() to about:blank#foo.
  let pushURL = "about:blank#foo";
  openedWindow.history.pushState({}, "title", pushURL);
  assert_equals(openedWindow.location.href, pushURL);
  assert_equals(history.length, 1,
    "history.length must not change after history.pushState() on the initial empty document");

  // Navigate away from the initial empty document through setting location.href.
  // This should do a replacement.
  openedWindow.location.href = url2;
  await waitForMessage(t, "loaded");
  assert_equals(openedWindow.history.length, 1,
    "history.length should not increase after normal navigation away from initial empty document");
}, "history.pushState");

promise_test(async t => {
  // Open a new window with a URL that doesn't load a new document, so it will stay in the initial empty document.
  const openedWindow = windowOpen204(t);

  // Do a history.pushState() to about:blank#foo.
  let replaceURL = "about:blank#foo";
  openedWindow.history.replaceState({}, "title", replaceURL);
  assert_equals(openedWindow.location.href, replaceURL);
  assert_equals(history.length, 1,
    "history.length must not change after history.replaceState() on the initial empty document");

  // Navigate away from the initial empty document through location.assign().
  // This should do a replacement.
  openedWindow.location.assign(url2);
  await waitForMessage(t, "loaded");
  assert_equals(openedWindow.history.length, 1,
    "history.length should not increase after normal navigation away from initial empty document");
}, "history.replaceState");
</script>
